import java.util.Stack;

/**
 * @User: vitobo
 * @Date: 2024-06-22
 * @Description: 最小栈 实现
 */
class MinStack {
    private Stack<Integer> stack1;
    private Stack<Integer> minStack;

    public MinStack(Stack<Integer> minStack) {
        stack1 = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val){
        stack1.push(val);
        if(minStack.empty()){
            minStack.push(val);
        }else {
            int x = minStack.peek();
            if(val <= x){
                minStack.push(val);
            }
        }
    }

    public void pop(){
        if(!stack1.empty()){
            int x = stack1.pop();
            if(x == minStack.peek()){
                minStack.pop();
            }
        }
    }

    // 获取栈顶元素
    public int top(){
        if(!stack1.empty()){
            return stack1.peek();
        }
        return -1;
    }

    public int getMin(){
        if(minStack.empty()){
            return -1;
        }
        return minStack.peek();
    }
}
